package HOPACH;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Paint;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

import org.apache.commons.collections15.Transformer;

import edu.uci.ics.jung.algorithms.layout.TreeLayout;
import edu.uci.ics.jung.graph.DelegateTree;
import edu.uci.ics.jung.visualization.BasicVisualizationServer;
import edu.uci.ics.jung.visualization.VisualizationViewer;
import edu.uci.ics.jung.visualization.control.DefaultModalGraphMouse;
import edu.uci.ics.jung.visualization.decorators.ToStringLabeller;
import edu.uci.ics.jung.visualization.renderers.Renderer.VertexLabel.Position;

public class GraphGUI {
	public DelegateTree<Cluster, MyLink> g;
	public static int edgeCount=0;
	public Icon icon;
	
	JFrame frame;
	
	public GraphGUI(Cluster root){
	
		
		this.setNewTree(root);
		
	}
	
	private void rekursivClusterAdd(DelegateTree<Cluster, MyLink> tree, Cluster parent){
		for(int i=0;i<parent.childCluster.size();i++){
			g.addChild(new MyLink(1, 1), parent, parent.childCluster.get(i));
			rekursivClusterAdd(g, parent.childCluster.get(i));
		}
	}
	
	public JPanel getJPanelGraph(){
		Transformer<Cluster,Paint> vertexPaint = new Transformer<Cluster,Paint>() {

			@Override
			public Paint transform(Cluster c) {
				if (c.averageSilhouette > 0)
					return Color.GREEN;
				if (c.averageSilhouette > -1)
					return Color.RED;
				return Color.YELLOW;
			}
		};
		
		
		// The Layout<V, E> is parameterized by the vertex and edge types
		TreeLayout<Cluster, MyLink> layout = new TreeLayout<Cluster, MyLink>(g);
		
//		layout.setSize(new Dimension(300, 300)); // sets the initial size of the
											// space
				
		// The BasicVisualizationServer<V,E> is parameterized by the edge types
//		BasicVisualizationServer<Cluster, MyLink> vv = new BasicVisualizationServer<Cluster, MyLink>(
//				layout);
		VisualizationViewer<Cluster, MyLink> vv = new VisualizationViewer<Cluster, MyLink>(
				layout);
		vv.setPreferredSize(new Dimension(350, 350)); // Sets the viewing area
		
		vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller<Cluster>());
//		vv.getRenderContext().setVertexLabelRenderer((VertexLabelRenderer) new BasicVertexLabelRenderer());
		
		vv.getRenderer().getVertexLabelRenderer().setPosition(Position.SE);
		
		vv.getRenderContext().setVertexFillPaintTransformer(vertexPaint);
		
		DefaultModalGraphMouse gm = new DefaultModalGraphMouse();
//		gm.setMode(g.Mode.TRANSFORMING);
		vv.setGraphMouse(gm);
		
//		try {
//			Image img = ImageIO.read(new File("C:\\Users\\Tony\\Desktop\\sarcasm.jpg"));
//			icon= new ImageIcon(img);
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
		
//		vv.getRenderContext().setVertexIconTransformer(new Transformer<MyNode, Icon>() {
//			
//			@Override
//			public Icon transform(MyNode arg0) {
//				
//				return icon;
//			}
//		});               
		
		return vv;
	}
	
	class MyLink {
		double capacity; // should be private
		double weight; // should be private for good practice
		int id;
		
		public MyLink(double weight, double capacity) {
			this.id = edgeCount++; // This is defined in the outer class.
			this.weight = weight;
			this.capacity = capacity;
		}

		public String toString() { // Always good for debugging
			return "E" + id;
		}
	}

	public void setNewTree(Cluster root){
		if(frame!=null)
			frame.setVisible(false);

		frame = new JFrame("Simple Graph View");		
		
		g = new DelegateTree<Cluster, MyLink>();
		// Add some directed edges along with the vertices to the graph
		g.setRoot(root);
		
		for(int i=0;i<root.childCluster.size();i++){
			g.addChild(new MyLink(1, 1), root, root.childCluster.get(i));
			rekursivClusterAdd(g,root.childCluster.get(i));
		}
		
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().add(this.getJPanelGraph());
		
//		JButton jbutton= new JButton("change");
//		jbutton.addActionListener(new ActionListener() {
//			public void actionPerformed(ActionEvent e) {
//				if(Cluster.ShowStatus==0)
//					Cluster.ShowStatus=1;
//				else
//					Cluster.ShowStatus=0;
//				
//			}
//			});
//		
//		frame.getContentPane().add(jbutton);
		frame.pack();
		frame.setVisible(true);
	}

	
	public static void main(String[] arg) throws IOException {
		GraphGUI sgv = new GraphGUI(new Cluster()); // We create our graph in
														// here
		
														// size
//		JFrame frame = new JFrame("Simple Graph View");
//		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//		frame.getContentPane().add(sgv.getJPanelGraph());
//		frame.pack();
//		frame.setVisible(true);
		
		Cluster c= new Cluster();
		c.members.add(7);
		sgv.setNewTree(c);		
	}

	
}
